package com.kky.pp.activity.login

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.airbnb.lottie.LottieAnimationView
import com.custom.base.base.SDBaseEvent
import com.custom.base.mvp.MVPBaseActivity
import com.custom.base.util.IntentUtil
import com.custom.base.util.ToastUtil
import com.google.android.gms.auth.api.identity.Identity
import com.google.android.gms.auth.api.identity.SignInClient
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.gyf.immersionbar.ImmersionBar
import com.kky.pp.R
import com.kky.pp.activity.login.phone.PhoneLoginActivity
import com.kky.pp.databinding.ActivityLoginBinding
import com.kky.pp.dot.DotLogEventName
import com.kky.pp.dot.DotLogUtil
import com.kky.pp.util.GoogleUtil
import com.kky.pp.util.SystemUtils
import io.rong.imkit.event.EnumEventTag


/**
 * 作者：
 * 时间：
 * 描述：登录
 */

class LoginActivity : MVPBaseActivity<LoginContract.View, LoginPresenter,ActivityLoginBinding>(ActivityLoginBinding::inflate), LoginContract.View {

    private lateinit var oneTapClient: SignInClient
    private lateinit var auth: FirebaseAuth
    private lateinit var googleSignInClient: GoogleSignInClient
    private val REQ_ONE_TAP = 2  // Can be any integer unique to the Activity

    override fun bindLayout(): Int {
        return R.layout.activity_login
    }

    override fun setFullScreen(): Boolean {
        return true
    }
    //实现“onRequestPermissionsResult”函数接收校验权限结果
    override fun onEventMainThread(event: SDBaseEvent) {
        when (EnumEventTag.valueOf(event.tagString)) {
            EnumEventTag.LOGIN_FINISH -> {
                finish()
            }
            else -> {}
        }
    }

    override fun initView() {
        googleSignInClient = GoogleUtil().googleLogin(this)
        auth = Firebase.auth
        oneTapClient = Identity.getSignInClient(this)
        linDLGoogle.setOnClickListener {
            if (SystemUtils.isConnected(mActivity)) {
                lottieDLLoadings.visibility= View.VISIBLE
                lottieDLLoadings.playAnimation()
                val signInIntent = googleSignInClient.signInIntent
                startActivityForResult(signInIntent, REQ_ONE_TAP)
            } else {
                ToastUtil.toast(getString(R.string.ooops_network_error))
            }
            DotLogUtil.setEventName(DotLogEventName.LOGING).add("type","googleLogin")?.commit()
        }
        viewBinding.txtDLPhone.setOnClickListener {
            DotLogUtil.setEventName(DotLogEventName.LOGING).add("type","phoneLogin")?.commit()
            IntentUtil.startActivity(PhoneLoginActivity::class.java)
        }
        viewBinding.frameDLRecallHint.setOnClickListener {
            it.isVisible = false
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            REQ_ONE_TAP -> {
                val task = GoogleSignIn.getSignedInAccountFromIntent(data)
                try {
                    val account = task.getResult(ApiException::class.java)!!
                    mPresenter?.getLoginToken(account,"2")
                } catch (e: ApiException) {
                    lottieDLLoadings.visibility=View.GONE
                    lottieDLLoadings.pauseAnimation()
                    showToast("Google sign in failed")
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(mActivity).statusBarDarkFont(false).init()
    }
    override fun onDestroy() {
        super.onDestroy()
        lottieDLLoadings.cancelAnimation()
    }
    override val txtDLProtocols: TextView
        get() = viewBinding.txtDLProtocol
    override val linDLGoogle: LinearLayout
        get() = viewBinding.linearDLGoogle
    override val txtDLPhones: TextView
        get() = viewBinding.txtDLPhone
    override val conDLContainers: ConstraintLayout
        get() = viewBinding.conDLContainer
    override val lottieDLLoadings: LottieAnimationView
        get() = viewBinding.lottieDLLoading
    override val frameDLRecallHints: FrameLayout
        get() = viewBinding.frameDLRecallHint
    override val tvDLRecallContents: TextView
        get() = viewBinding.tvDLRecallContent

}